﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace CustomerService
{
    public partial class OrderDetailTab : System.Web.UI.UserControl
    {
        private SalesManagerEntities _context;
        protected tblOrderHeader _orderHeader = _emptyOrder;
        private static tblOrderHeader _emptyOrder = new tblOrderHeader();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                // Load the values from the listview back into the entity context
                for (int i = 0; i < ListView1.Items.Count; i++)
                {
                    int key = (int)ListView1.DataKeys[i].Value;
                    tblOrderDetail orderDetail = (tblOrderDetail)_context.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.tblOrderDetail", "OrderDetailID", key));
                    int qty;
                    if (int.TryParse(((TextBox)ListView1.Items[i].FindControl("txtItemQty")).Text, out qty))
                        orderDetail.Qty = qty;
                    double discount;
                    if (double.TryParse(((TextBox)ListView1.Items[i].FindControl("txtDiscount")).Text, 
                        System.Globalization.NumberStyles.AllowCurrencySymbol | System.Globalization.NumberStyles.AllowDecimalPoint,
                        System.Globalization.CultureInfo.CurrentCulture,
                        out discount))
                        orderDetail.Discount = discount;
                }
            _orderHeader.ProjShipDate = DateTime.Parse(txtProjShipDate.Text);
            _orderHeader.Shipping = decimal.Parse(txtShipping.Text, System.Globalization.NumberStyles.AllowCurrencySymbol | System.Globalization.NumberStyles.AllowDecimalPoint);
            }
        }

        public new void Load(SalesManagerEntities context, tblOrderHeader orderHeader, bool forceUpdate)
        {
            _context = context;
            _orderHeader = orderHeader;
            txtOrderID.Text = _orderHeader.OrderID.ToString();
            btnCancelOrder.Disabled = !_orderHeader.CanCancel;
            btnAddItem.Disabled = !_orderHeader.CanEdit;

            if (!IsPostBack || forceUpdate)
            {
                txtProjShipDate.Text = orderHeader.ProjShipDate.ToShortDateString();
                txtProjShipDate.Enabled = orderHeader.CanEdit;
                txtShipping.Text = orderHeader.Shipping.ToString("C");
                txtShipping.Enabled = orderHeader.CanEdit;
            }
            if (!IsPostBack || forceUpdate || (Request.Params["__EVENTARGUMENT"] == "UpdateDetails"))
            {
                ListView1.DataSource = _orderHeader.Customer.cachedOrderDetails.Where(t => t.OrderHeader == _orderHeader);
                ListView1.DataBind();
            }
        }

        protected void btnRemove_Click(object sender, CommandEventArgs e)
        {
            int key = int.Parse(e.CommandArgument.ToString());
            _context.DeleteObject(_context.GetObjectByKey(new System.Data.EntityKey("SalesManagerEntities.tblOrderDetail", "OrderDetailID", key)));
            _context.SaveChanges();
            ListView1.DataSource = _orderHeader.Customer.cachedOrderDetails.Where(t => t.OrderHeader == _orderHeader);
            ListView1.DataBind();
        }
    }
}